$(function () {
    $("#jqGrid").jqGrid({
        url: 'table/page',
        datatype: "json",
        colModel: [
            { label: '数据源', name: 'ds', width: 50, key: true },
            { label: 'SCHEMA', name: 'schemaName', width: 100, key: true },
			{ label: '表名', name: 'tableName', width: 100, key: true },
			{ label: 'Engine', name: 'engine', width: 50},
			{ label: '表备注', name: 'tableComment', width: 100 },
			{ label: '创建时间', name: 'createTime', width: 100 }
        ],
        loadComplete: function (data) {
            if(data.code != 0){
                console.log(data)
                window.alert(data.msg)
            }
        },
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			tableName: null
		},
        dsList:[
        ],
        ds: '',
        dtId:null,
        dtList:[]
	},
    created() {
        this.queryDsList();
    },
	methods: {
		query: function () {
            console.log("数据源方法--------")
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'tableName': vm.q.tableName,'ds': vm.ds},
                page:1 
            }).trigger("reloadGrid");
            this.queryDtList(vm.ds)
		},
		generator: function() {
            var tableNames = getSelectedRows();
            if(tableNames == null){
                return ;
            }
            var url = "table/code?tableNames=" + tableNames.join() + "&ds=" + vm.ds;
            if(vm.dtId != null){
                url = url + "&templateId=" + vm.dtId;
            }
            location.href = url;
		},
        queryDsList:function(){
            fetch(localUrl + "/ds/list").then(res => res.json()).then(res => {
                //跨域问题：前端端口8080，后端端口9090，导致跨域
                if(res.code != 0){
                    window.alert(res.msg)
                } else {
                    var dataRList = res.data;
                    vm.dsList = []
                    for(var i = 0; i < dataRList.length; i++){
                        var ii = dataRList[i];
                        if(ii.primary){
                            vm.ds = ii.ds
                        }
                        vm.dsList.push(ii.ds)
                    }
                }
            })
        },
        queryDtList:function(){
            fetch(localUrl + "/template/list?&ds=" + vm.ds).then(res => res.json()).then(res => {
                //跨域问题：前端端口8080，后端端口9090，导致跨域
                if(res.code != 0){
                    window.alert(res.msg)
                } else {
                    var dataRList = res.data;
                    vm.dtList = []
                    for(var i = 0; i < dataRList.length; i++){
                        var ii = dataRList[i];
                        vm.dtList.push(ii)
                    }
                }
            });
        },
        clear:function () {
            vm.q.tableName = null
            vm.dtId = null
            this.query()
        }
	}
});

